package leetcode.editor.cn;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author: yangwk
 * @date: 2023/3/20
 * @description:
 **/
public class P513FindBottomLeftTreeValue {
    public static void main(String[] args) {

    }

    public static int findBottomLeftValue(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        TreeNode last = null;
        while (!queue.isEmpty()) {
            int size = queue.size();
            last = queue.peek();
            while (size-- > 0) {
                TreeNode poll = queue.poll();
                if (poll.left != null) {
                    queue.offer(poll.left);
                }
                if (poll.right != null) {
                    queue.offer(poll.right);
                }
            }
        }
        return last.val;
    }
}
